package com.ms.tank.abstractfactory;

import com.ms.tank.common.PropertyMgr;
import com.ms.tank.common.ResourceManager;
import com.ms.tank.enums.Dir;
import com.ms.tank.enums.Group;
import com.ms.tank.fire.FireStrategy;
import com.ms.tank.game.Bullet;
import com.ms.tank.game.TankFrame;
import com.ms.tank.utils.Audio;

import java.awt.*;
import java.util.Random;

/**
 * @Date 2021/3/18 18:40:29
 * @Author mashuai
 */
public class RectTank extends BaseTank{
    public int x, y;
    public Dir dir = Dir.UP;
    //移动值
    private final int SPEED = Integer.parseInt(PropertyMgr.get("tankSpeed").toString());
    public TankFrame tankFrame;
    public static int WIDTH = ResourceManager.goodTankU.getWidth();
    public static int HEIGHT = ResourceManager.goodTankU.getHeight();
    //是否移动
    private boolean moving = true;
    //是否存活
    private boolean living = true;
    public Group group = Group.BAD;
    private Random random = new Random();
    public FireStrategy fs;


    public RectTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tankFrame = tankFrame;
        this.group = group;

        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.height = HEIGHT;
        rectangle.width = WIDTH;
        try {
            if (group == Group.GOOD) {
                String goodFSName = PropertyMgr.get("goodFS").toString();

                fs = (FireStrategy) Class.forName(goodFSName).getDeclaredConstructor().newInstance();

            } else {
                String badFSName = PropertyMgr.get("badFS").toString();
                fs = (FireStrategy) Class.forName(badFSName).getDeclaredConstructor().newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void paint(Graphics g) {
        //如果当前坦克不存活则移除坦克列表中的当前坦克对象
        if (!living) tankFrame.tanks.remove(this);
        Color c = g.getColor();
        g.setColor(group == Group.GOOD ? Color.RED : Color.BLUE);
        g.fillRect(x, y, 40, 40);
        g.setColor(c);
        //移动方法
        move();
    }


    private void move() {
        //判断是否移动状态
        if (!moving) {
            return;
        }

        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }
        //随机发火
        if (this.group == Group.BAD && random.nextInt(100) > 80) {
            this.fire();
        }
        //随机移动
        if (this.group == Group.BAD && random.nextInt(100) > 95) {
            randomDir();
        }
        //边界检测
        boundsCheck();
        rectangle.x = this.x;
        rectangle.y = this.y;
    }

    private void boundsCheck() {
        if (this.x < 2) {
            x = 2;
        }
        if (this.y < 28) {
            y = 28;
        }
        if (this.x > TankFrame.GAME_WIDTH - RectTank.WIDTH) {
            x = TankFrame.GAME_WIDTH - RectTank.WIDTH;
        }
        if (this.y > TankFrame.GAME_HEIGHT - RectTank.HEIGHT) {
            y = TankFrame.GAME_HEIGHT - RectTank.HEIGHT;
        }
    }

    //随机移动
    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void fire() {
        //开火时调用子弹 子弹从中心发出
        int bX = this.x + RectTank.WIDTH/2 - Bullet.WIDTH/2;
        int bY = this.y + RectTank.HEIGHT/2 - Bullet.HEIGHT/2;

        Dir[] dirs = Dir.values();
        for(Dir dir : dirs) {
            tankFrame.gameFactory.createBullet(bX, bY, dir, group, tankFrame);
        }

        if(group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
    }

    //坦克死亡方法
    @Override
    public void die() {
        this.living = false;
    }






    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    public void setGroup(Group group) {
        this.group = group;
    }


    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getSPEED() {
        return SPEED;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

}
